package com.liangzhong.proxy.match
{
	import com.liangzhong.commandconst.match.CMD_Match;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.match.EventDes;
	import com.liangzhong.model.match.GameDes;
	import com.liangzhong.model.match.GameDes_Status;
	import com.liangzhong.model.match.GameStart;
	import com.liangzhong.model.match.MatchServerDataParse;
	import com.liangzhong.model.match.ShootBall_S;
	import com.liangzhong.model.match.vo.ChangePlayerPosVo;
	import com.liangzhong.model.match.vo.GameEndVo;
	import com.liangzhong.model.match.vo.PlayerSayVo;
	import com.liangzhong.model.match.vo.SelectTacticVo;
	import com.liangzhong.proxy.BaseProxy;
	import com.socket.clientsocket.IClientSocket;
	import com.socket.clientsocket.IClientSocketHandler;
	
	import flash.utils.ByteArray;

	/**
	 * 比赛
	 * @author lxh
	 * 
	 */	
	public class MatchProxy extends BaseProxy implements IClientSocketHandler
	{
		public static const ProxyName :String = "MatchProxy";
		/**
		 * 比赛开始结果
		 */		
		public static const GameStart_Result :String = "Server/Match/GameStart_Result";
		/**
		 *比赛事件结果 
		 */		
		public static const GameDes_Event_Result 	:String = "Server/Match/GameDes_Event_Result";
		/**
		 * 绝对状态结果
		 */		
		public static const GameDes_Status_Result	   :String = "Server/Match/GameDes_Status_Result";
		/**
		 * 比赛描述结果
		 */		
		public static const GameDes_Result	   :String = "Server/Match/GameDes_Result";
		/**
		 * 射门结果
		 */		
		public static const ShootBall_Result	   :String = "Server/Match/ShootBall_Result";
		/**
		 *战术结果 
		 */		
		public static const SelectTactics_Result	:String = "Server/SelectTactics_Result";
		/**
		 *位置结果 
		 */		
		public static const AdjustPlace_Result		:String = "Server/AdjustPlace_Result";
		/**
		 * 比赛结束
		 */		
		public static const GameEnd_Result			:String = "Server/Match/GameEnd_Result";
		/**
		 *球员说话 
		 */		
		public static const PlayerSaying_Result	:String = "Server/Match/PlayerSaying_Result";
		/**
		 *主要防守者结果 
		 */		
		public static const Main_Guard_Result		:String = "Server/Main_Guard_Result";
		
		public function MatchProxy(proxyName:String = ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			/*switch(mainCmdID)
			{
				case CMD_Match.MAIN:
				{
					return onDeal(mainCmdID,subCmdID,buffer,dataSize);
				}
			}*/
			if(mainCmdID >= 100 && mainCmdID <= 199)
			{
				CMD_Match.MAIN = mainCmdID;
				return onDeal(mainCmdID,subCmdID,buffer,dataSize);
			}
			return false;
		}
		/**
		 * 处理信息
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(mainCmdID:uint,
									 subCmdID:uint,
									 buffer:ByteArray, 
									 dataSize:int):Boolean
		{
			var matchId :int;
			var playerId :int;
			switch(subCmdID)
			{
				case CMD_Match.SUB_GameStart://比赛开始
				{
					var gameStart :GameStart = new GameStart();
					gameStart.readData(buffer);
					AppData.getInstance().matchDataManager.putIsGameStart(String(gameStart.matchId),true);
					//AppData.getInstance().matchDataManager.putGameStart(gameStart.matchId,gameStart);
					if(gameStart.bankerUserId == AppData.getInstance().user.uid)
					{
						AppData.getInstance().matchDataManager.isHome[gameStart.matchId] = true;
						gameStart.isHome = true;
					}
					else
					{
						AppData.getInstance().matchDataManager.isHome[gameStart.matchId] = false;
						gameStart.isHome = false;
					}
					this.sendNotification(GameStart_Result + gameStart.matchId,gameStart);
					//trace("GameStart:",gameStart.matchId);
					//
					//AppData.getInstance().matchDataManager.historyList.push(AppData.getInstance().stadiumDataManager.selfMatchInfo);
					//AppData.getInstance().matchDataManager.historyList.push(AppData.getInstance().stadiumDataManager.opponentMatchInfo);
					//AppData.getInstance().matchDataManager.historyList.push(gameStart);
					return true;
				}
				case CMD_Match.SUB_GameDes_Event://比赛事件
				{
					var eventDes :EventDes = new EventDes();
					eventDes.readData(buffer);
					this.sendNotification(GameDes_Event_Result + eventDes.matchId,eventDes);
					//trace("GameDes_Event:",eventDes.matchId);
					//AppData.getInstance().matchDataManager.historyList.push(eventDes);
					return true;
				}
				case CMD_Match.SUB_GameDes_Status://绝对状态
				{
					var gameDes_Status :GameDes_Status = new GameDes_Status();
					gameDes_Status.readData(buffer);
					this.sendNotification(GameDes_Status_Result + gameDes_Status.matchId,gameDes_Status);
					//trace("GameDes_Status:",gameDes_Status.matchId);
					//AppData.getInstance().matchDataManager.historyList.push(gameDes_Status);
					return true;
				}
				case CMD_Match.SUB_GameDes_Action://比赛描叙
				{
					var gameDes :GameDes = new GameDes();
					gameDes.readData(buffer);
					this.sendNotification(GameDes_Result + gameDes.matchId,gameDes);
					//AppData.getInstance().matchDataManager.historyList.push(gameDes);
					return true;
				}
				case CMD_Match.SUB_Shoot_Ball://射门
				{
					var shootBall_S :ShootBall_S = new ShootBall_S();
					shootBall_S.readData(buffer);
					this.sendNotification(ShootBall_Result + shootBall_S.matchId,shootBall_S);
					return true;
				}
				case CMD_Match.SUB_Select_Tactics://选择战术
				{
					var selectTacticVo :SelectTacticVo = MatchServerDataParse.readSelectStatics(buffer);;
					this.sendNotification(SelectTactics_Result + selectTacticVo.matchId,selectTacticVo);
					return true;
				}
				case CMD_Match.SUB_ChangePlayerPos://调整位置
				{
					var changePlayerPosVo :ChangePlayerPosVo = MatchServerDataParse.readAdjustPlayer(buffer);
					this.sendNotification(AdjustPlace_Result + changePlayerPosVo.matchId,changePlayerPosVo);
					return true;
				}
				case CMD_Match.SUB_Player_Saying://球员冒泡事件
				{
					var playerSayVo :PlayerSayVo = MatchServerDataParse.readPlayerSay(buffer);
					this.sendNotification(PlayerSaying_Result + playerSayVo.matchId,playerSayVo);
					return true;
				}
				case CMD_Match.SUB_GameEnd://比赛结束
				{
					var gameEndVo :GameEndVo = MatchServerDataParse.readGameEnd(buffer);
					this.sendNotification(GameEnd_Result + gameEndVo.matchId,gameEndVo);
					this.sendNotification(NotificationConstants.MATCH_SERVER_END,{matchId:gameEndVo.matchId});
					//AppData.getInstance().matchDataManager.historyList.push(gameEndVo);
					//保存比赛录像
					//Debug.logGameHistory(AppData.getInstance().user.uid,AppData.getInstance().matchInfo.matchId,JSON.encode(AppData.getInstance().matchDataManager.historyList));
					//AppData.getInstance().matchDataManager.historyList = [];
					return true;
				}
				case CMD_Match.SUB_Main_Guard://主要防守者
				{
					matchId = buffer.readInt();
					playerId = buffer.readInt();
					this.sendNotification(Main_Guard_Result + matchId,{matchId:matchId,pid:playerId});
					return true;
				}
			}
			return false;
		}
		/**
		 *资源加载完成，准备好了 
		 * @param userId
		 * @param matchId
		 * 
		 */		
		public function loadResComplete(param:Object):void
		{
			var matchId :int = param.matchId;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,CMD_Match.MAIN,CMD_Match.SUB_LoadRes_Complete);
			sendBuf.writeUnsignedInt(matchId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *射门 
		 * @param userId
		 * 
		 */		
		public function shootBallReq(param:Object):void
		{
			var matchId :int = param.matchId;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,CMD_Match.MAIN,CMD_Match.SUB_Shoot_Ball);
			sendBuf.writeUnsignedInt(matchId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *选择战术 
		 * @param userId
		 * @param type
		 * 
		 */		
		public function selectTactics(param:Object):void
		{
			var type :int = param.type;
			var matchId :int = param.matchId;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4 + 1,CMD_Match.MAIN,CMD_Match.SUB_Select_Tactics);
			sendBuf.writeUnsignedInt(matchId);
			sendBuf.writeByte(type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *调整位置 
		 * @param userId
		 * @param posArr
		 * 
		 */		
		public function adjustPlace(param:Object):void
		{
			var matchId :int = param.matchId;
			var posArr :Array = param.posArr;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 48,CMD_Match.MAIN,CMD_Match.SUB_ChangePlayerPos);
			sendBuf.writeUnsignedInt(matchId);
			for each(var parm :Object in posArr)
			{
				sendBuf.writeShort(int(parm.x));
				sendBuf.writeShort(int(parm.y));
				//trace(int(parm.x),int(parm.y));
			}
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *聊天 
		 * @param value
		 * 
		 */		
		public function chat(value :Object):void
		{
			var matchId :int = value.matchId;
			var sayId :int = value.sayId;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4 + 4,CMD_Match.MAIN,CMD_Match.SUB_Player_Chat);
			sendBuf.writeInt(matchId);
			sendBuf.writeInt(sayId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *请求铲球 
		 * @param value
		 * 
		 */		
		public function slide(value :Object):void
		{
			var matchId :int = value.matchId;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,CMD_Match.MAIN,CMD_Match.SUB_Slide,0);
			sendBuf.writeInt(matchId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *请求突破 
		 * @param value
		 * 
		 */		
		public function cross(value :Object):void
		{
			var matchId :int = value.matchId;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,CMD_Match.MAIN,CMD_Match.SUB_Cross,0);
			sendBuf.writeInt(matchId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
	}
}